#!/usr/bin/perl
#
# Patch f_mtime for objects that have f_ctime but not f_mtime
#
use FileTable;
use HTTP::Date;
use Fcntl;

$DEBUG = 0;

$filename = shift;
$reccount = shift;
$reccount = 100 if !$reccount;

if (! -f "$filename.conf") {
    $filename = $ENV{"COUNTER_ROOT"} . "/db/" . $filename;
}

$file = FileTable::open($filename, O_RDWR);
@recs = $file->select("f_ctime > '1990-01-01' and f_mtime is null limit $reccount");
warn "Number of records is ", @recs + 0, "\n";
for $rec (@recs) {
    if (!$rec->{f_ctime}) {
	die "Null f_ctime on ", $rec->key, "\n";
    }
    $rec->{f_mtime} = $rec->{f_ctime};
    print $rec->key(), ":", "Set mtime to $$rec{f_ctime}\n" if $DEBUG;
    # Extremely dirty trick: Bypass the timestamping
    bless $rec, FileTable::Record;
    $rec->store("NOTEXT");
    ++ $act{changed};
}
for $act (keys(%act)) {
    print $act{$act}, " ", $act, "\n";
}

